package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.system.train.info.SupplierSystemOrderTrainInfoDTO;
import com.ctshk.rpc.booking.dto.system.train.info.SupplierSystemOrderTrainTicketInfoDTO;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAdmissionTicketDetail;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderTrain;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderTrainTicket;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderTrain;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderTrainMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderTrainTicketMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderTrainMapper;
import com.ctshk.rpc.booking.req.system.train.add.SupplierSystemOrderTrainAddReq;
import com.ctshk.rpc.booking.req.system.train.add.SupplierSystemOrderTrainTicketAddReq;
import com.ctshk.rpc.booking.req.system.train.edit.SupplierSystemOrderTrainEditReq;
import com.ctshk.rpc.booking.service.ISupplierSystemOrderTrainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统火车票订单 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierSystemOrderTrainServiceImpl implements ISupplierSystemOrderTrainService {
    @Autowired
    SupplierSystemOrderTrainMapper supplierSystemOrderTrainMapper;
    @Autowired
    SupplierSystemOrderTrainTicketMapper supplierSystemOrderTrainTicketMapper;

    @Override
    public void update(Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderTrainEditReq> supplierSystemOrderTrainEditReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderTrainEditReqList)) {
            supplierSystemOrderTrainEditReqList.forEach(trainEditReq -> {
                UpdateWrapper<SupplierSystemOrderTrain> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(SupplierSystemOrderTrain::getId, trainEditReq.getId());
                updateWrapper.lambda().set(SupplierSystemOrderTrain::getCustomerRemark, trainEditReq.getCustomerRemark());
                updateWrapper.lambda().set(SupplierSystemOrderTrain::getSupplierRemark, trainEditReq.getSupplierRemark());
                updateWrapper.lambda().set(SupplierSystemOrderTrain::getModifiedId, userId);
                updateWrapper.lambda().set(SupplierSystemOrderTrain::getGmtModified, nowDateTime);
                supplierSystemOrderTrainMapper.update(new SupplierSystemOrderTrain(), updateWrapper);
                if (CollectionUtils.isNotEmpty(trainEditReq.getSupplierSystemOrderTrainTicketEditReqList())) {
                    trainEditReq.getSupplierSystemOrderTrainTicketEditReqList().forEach(trainTicketEditReq -> {
                        UpdateWrapper<SupplierSystemOrderTrainTicket> updateTicketWrapper = new UpdateWrapper<>();
                        updateTicketWrapper.lambda().eq(SupplierSystemOrderTrainTicket::getId, trainTicketEditReq.getId());
                        updateTicketWrapper.lambda().set(SupplierSystemOrderTrainTicket::getSinglePrice, trainTicketEditReq.getSinglePrice());
                        updateTicketWrapper.lambda().set(SupplierSystemOrderTrainTicket::getModifiedId, userId);
                        updateTicketWrapper.lambda().set(SupplierSystemOrderTrainTicket::getGmtModified, nowDateTime);
                        supplierSystemOrderTrainTicketMapper.update(null, updateTicketWrapper);
                    });
                }
            });
        }
    }

    @Override
    public void add(Long id, Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderTrainAddReq> supplierSystemOrderTrainAddReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderTrainAddReqList)) {
            List<SupplierSystemOrderTrain> supplierSystemOrderTrainList = new ArrayList<>();
            List<SupplierSystemOrderTrainTicket> supplierSystemOrderTrainTicketList = new ArrayList<>();
            supplierSystemOrderTrainAddReqList.forEach(trainAddReq -> {
                SupplierSystemOrderTrain supplierSystemOrderTrain = EntityUtil.copy(trainAddReq, SupplierSystemOrderTrain.class);
                supplierSystemOrderTrain.setId(SnowflakeIdWorker.nextId());
                supplierSystemOrderTrain.setOrderId(id);
                supplierSystemOrderTrain.setCreateId(userId);
                supplierSystemOrderTrain.setGmtCreate(nowDateTime);
                supplierSystemOrderTrain.setModifiedId(userId);
                supplierSystemOrderTrain.setGmtModified(nowDateTime);
                supplierSystemOrderTrain.setIsDeleted(IsDeletedCode.NO.getCode());
                supplierSystemOrderTrainList.add(supplierSystemOrderTrain);
                //添加旅客
                List<SupplierSystemOrderTrainTicketAddReq> supplierSystemOrderTrainTicketAddReqList = trainAddReq.getSupplierSystemOrderTrainTicketAddReqList();
                if (CollectionUtils.isNotEmpty(supplierSystemOrderTrainTicketAddReqList)) {
                    supplierSystemOrderTrainTicketAddReqList.forEach(trainTouristAdd -> {
                        SupplierSystemOrderTrainTicket supplierSystemOrderTrainTicket = EntityUtil.copy(trainTouristAdd, SupplierSystemOrderTrainTicket.class);
                        supplierSystemOrderTrainTicket.setId(SnowflakeIdWorker.nextId());
                        supplierSystemOrderTrainTicket.setOrderId(id);
                        supplierSystemOrderTrainTicket.setTrainId(supplierSystemOrderTrain.getId());
                        supplierSystemOrderTrainTicket.setCreateId(userId);
                        supplierSystemOrderTrainTicket.setGmtCreate(nowDateTime);
                        supplierSystemOrderTrainTicket.setModifiedId(userId);
                        supplierSystemOrderTrainTicket.setGmtModified(nowDateTime);
                        supplierSystemOrderTrainTicket.setIsDeleted(IsDeletedCode.NO.getCode());
                        supplierSystemOrderTrainTicketList.add(supplierSystemOrderTrainTicket);
                    });
                }
            });
            supplierSystemOrderTrainMapper.insertBatchSomeColumn(supplierSystemOrderTrainList);
            if (CollectionUtils.isNotEmpty(supplierSystemOrderTrainTicketList)) {
                supplierSystemOrderTrainTicketMapper.insertBatchSomeColumn(supplierSystemOrderTrainTicketList);
            }
        }
    }

    @Override
    public List<SupplierSystemOrderTrainInfoDTO> selectByOrderId(Long id) {
        QueryWrapper<SupplierSystemOrderTrain> trainQueryWrapper = new QueryWrapper<>();
        trainQueryWrapper.lambda().eq(SupplierSystemOrderTrain::getOrderId, id);
        List<SupplierSystemOrderTrain> supplierSystemOrderTrainList = supplierSystemOrderTrainMapper.selectList(trainQueryWrapper);
        List<SupplierSystemOrderTrainInfoDTO> supplierSystemOrderTrainInfoDTOList = EntityUtil.copyList(supplierSystemOrderTrainList, SupplierSystemOrderTrainInfoDTO.class);
        if (CollectionUtils.isNotEmpty(supplierSystemOrderTrainInfoDTOList)) {
            supplierSystemOrderTrainInfoDTOList.forEach(trainCheckinDTO -> {
                //添加旅客
                QueryWrapper<SupplierSystemOrderTrainTicket> trainTouristQueryWrapper = new QueryWrapper<>();
                trainTouristQueryWrapper.lambda().eq(SupplierSystemOrderTrainTicket::getOrderId, id);
                List<SupplierSystemOrderTrainTicket> supplierSystemOrderTrainTickets = supplierSystemOrderTrainTicketMapper.selectList(trainTouristQueryWrapper);
                List<SupplierSystemOrderTrainTicketInfoDTO> supplierSystemOrderTrainTicketInfoDTOS = EntityUtil.copyList(supplierSystemOrderTrainTickets, SupplierSystemOrderTrainTicketInfoDTO.class);
                trainCheckinDTO.setSupplierSystemOrderTrainTicketInfoDTOList(supplierSystemOrderTrainTicketInfoDTOS);
            });
        }
        return supplierSystemOrderTrainInfoDTOList;
    }
}
